package net.sumcraft.mythicmobitemhandler;

import io.lumine.xikage.mythicmobs.MythicMobs;
import io.lumine.xikage.mythicmobs.io.IOHandler;
import io.lumine.xikage.mythicmobs.io.IOLoader;
import io.lumine.xikage.mythicmobs.items.ItemManager;
import io.lumine.xikage.mythicmobs.items.MythicItem;
import net.sumcraft.mythicmobitemhandler.commands.ItemSaverCommand;
import net.sumcraft.mythicmobitemhandler.listener.MobDeathListener;
import net.sumcraft.mythicmobitemhandler.util.ConfigManager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author sumdream
 * @date 2018/4/27
 */
public class MythicMobItemHandler extends JavaPlugin {
    public static FileConfiguration config;
    /**
     * mm物品管理器
     */
    private ItemManager itemManager;
    /**
     * mm默认物品
     */
    private IOLoader defaultItems;
    /**
     * mm物品文件容器
     */
    private List<File> itemFiles;
    /**
     * key=物品文件名 value是该文件下的物品List
     */
    private HashMap<String, List<MythicItem>> itemMap;
    public static MythicMobItemHandler mythicMobItemHandler;

    @Override
    public void onEnable() {
        // Plugin startup logic
        mythicMobItemHandler = this;
        if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null) {
            getLogger().info("对不起,缺少前置插件 PlaceholderAPI");
        }
        if (Bukkit.getPluginManager().getPlugin("MythicMobs") == null) {
            getLogger().info("对不起,缺少前置插件 MythicMobs");
            Bukkit.getPluginManager().disablePlugins();
        }
        this.getCommand("mmits").setExecutor(new ItemSaverCommand());
        getServer().getPluginManager().registerEvents(new MobDeathListener(), this);
        load();
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
    }

    public void load() {
        saveDefaultConfig();
        reloadConfig();
        config = getConfig();
        itemMap = new HashMap<>();
        this.setItemManager(MythicMobs.inst().getItemManager());
        defaultItems = new IOLoader(MythicMobs.inst(), "ExampleItems.yml", "Items");
        this.setItemFiles(IOHandler.getAllFiles(defaultItems.getFile().getParent()));
        for (File file : itemFiles
        ) {
            String fileName = file.getName();
            if (!itemMap.containsKey(fileName)) {
                itemMap.put(fileName, new ArrayList<MythicItem>());
            }
            for (MythicItem mItem : itemManager.getItems()
            ) {
                if (mItem.getFile().equalsIgnoreCase(fileName)) {
                    itemMap.get(fileName).add(mItem);
                }
            }
        }
        ConfigManager.drop_to_inv = config.getBoolean("drop_to_inv");
        ConfigManager.spilt_key = config.getString("split_key");
        ConfigManager.ingore_slot = config.getIntegerList("ingore_slot");
        ConfigManager.enable_random_lore = config.getBoolean("enable_random_lore");
        ConfigManager.enable_level_lore = config.getBoolean("enable_level_replace");
        ConfigManager.enable_PAPI_lore = config.getBoolean("enable_PAPI_replace");
    }

    public ItemManager getItemManager() {
        return itemManager;
    }

    public void setItemManager(ItemManager itemManager) {
        this.itemManager = itemManager;
    }

    public IOLoader getDefaultItems() {
        return defaultItems;
    }

    public void setDefaultItems(IOLoader defaultItems) {
        this.defaultItems = defaultItems;
    }

    public List<File> getItemFiles() {
        return itemFiles;
    }

    public void setItemFiles(List<File> itemFiles) {
        this.itemFiles = itemFiles;
    }

    public HashMap<String, List<MythicItem>> getItemMap() {
        return itemMap;
    }

    public void setItemMap(HashMap<String, List<MythicItem>> itemMap) {
        this.itemMap = itemMap;
    }

}
